package Exercise;

import java.util.Random;

/**
 * Description：
 * Author: zhangc
 * Date：2017/8/29 14:34
 */
public class DuoyiSwap {

    public static int swapTime(int n) {
        int[] arr = new int[ n ];
        int result = 0;
        for (int i = 0; i < n; i++) {
            arr[ i ] = i;
        }
        boolean status = true;
        int j;
        while (status) {
            for (int i = 0; i < n; i++) {
                j = i + 1;
                if (j < n && arr[ i ] < arr[ j ]) {
                    swap(arr, i, j);
                    ++i;
                    status = false;
                }
            }
            if (status)
                break;
            status = true;
            result++;
        }
        return result;
    }

    public static int getNum(int n) {
        if (n <= 2)
            return n - 1;
        else
            return n;
    }

    public static void swap(int[] arr, int a, int b) {
        int temp = arr[ a ];
        arr[ a ] = arr[ b ];
        arr[ b ] = temp;
    }

    public static void main(String[] args) {
        Random random = new Random();
        for (int i = 1; i < 2000; i++) {
            int i2 = random.nextInt(20000);
            int i1 = swapTime(i2);
            if (getNum(i2) != i1)
                System.out.println("What a fuck day!!!");
        }
    }

}
